package com.cxf.windex.rest;

import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.DateUtil;
import com.cxf.windex.biz.WeiTypeBiz;
import com.cxf.windex.entity.WeiType;
import com.cxf.windex.entity.WeiUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author chenxf
 * @date 4/22/2018
 * @description
 */
@Controller
@RequestMapping("type")
public class WeiTypeController extends BaseRestController<WeiTypeBiz,WeiType>{
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<WeiType> list(
      @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
       String name,String code) {

    Example example = new Example(WeiType.class);
    Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(name)) {
      criteria.
          andLike("name", "%" + name + "%");
    }
    if (StringUtils.isNotBlank(code)) {
      criteria.
          andEqualTo("code", code );
    }

    example.setOrderByClause("crt_time desc ");
    PageHelper.startPage(offset, limit);
    List<WeiType> items = baseBiz.selectByExample(example);

    PageInfo<WeiType> pageInfo = new PageInfo<WeiType>(items);
    return new TableResultResponse<WeiType>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }
}
